package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.pojo.Setmeal;
import com.health.service.SetMealService;
import com.health.utils.QiNiuUtil;
import com.health.vo.PageResult;
import com.health.vo.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * setMeal表控制层
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    /**
     * 编辑套餐信息-修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        int count=setMealService.update(setmeal,checkGroupIds);

        if (count>0){
            return new Result(true,"修稿套餐信息成功");
        }
        return new Result(false,"修改套餐信息失败");
    }

    /**
     * 根据id查询套餐关联的检查组id-数据回显
     */
    @GetMapping("/findCheckGroupIds/{id}")
    public Result findCheckGroupIds(@PathVariable Integer id){
        List<Integer> checkGroupIds= setMealService.findCheckGroupIds(id);

        return new Result(true,"关联的检查组id查询成功",checkGroupIds);
    }
    /**
     * 根据id查询套餐-数据回显
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        Setmeal setmeal=setMealService.findById(id);

        //封装套餐和图片的域名
        Map<String,Object> map=new HashMap<>();
        map.put("setmeal",setmeal);
        map.put("domain",QiNiuUtil.DOMAIN);

        return new Result(true,"套餐查询成功",map);
    }


    /**
     * 添加套餐信息
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')") //拥有SETMEAL_ADD权限才能访问
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        int count=setMealService.add(setmeal,checkGroupIds);
        if (count>0){
            return new Result(true,"添加套餐成功");
        }
        return new Result(false,"添加套餐失败");
    }

    /**
     * 上传套餐图片
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {
        //使用七牛云保存图片
        String imgName = QiNiuUtil.uploadFile(imgFile.getInputStream());
        //封装文件名和域名
        Map<String,Object> map=new HashMap<String, Object>();

        map.put("imgName",imgName);
        map.put("domain",QiNiuUtil.DOMAIN);

        return new Result(true,"图片上传成功",map);
    }

    /**
     * 套餐删除
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN')") //拥有角色ROLE_ADMIN才能访问
    public Result deleteById(@PathVariable Integer id){
        int count=setMealService.deleteById(id);

        if (count>0){
            return new Result(true,"套餐删除成功");
        }
        return new Result(false,"套餐删除失败");
    }
    /**
     * 套餐分页查询
     */
    @GetMapping("/findPage/{pageNow}/{pageSize}")
    public Result findPage(@PathVariable Integer pageNow,@PathVariable Integer pageSize,String query){
        PageResult<Setmeal> pageResult=setMealService.findPage(pageNow,pageSize,query);

        return new Result(true,"套餐分页查询成功",pageResult);
    }

}
